<?php

namespace app\admin\controller\yxerp;

use app\common\controller\Backend;
use think\facade\Db;
use think\facade\Validate;
use app\admin\model\yxerp\PermissionGroup as PermissionGroupModel;

/**
 * 权限组管理
 */
class PermissionGroup extends Backend
{
    /**
     * 模型对象
     * @var object
     */
    protected $model = null;

    /**
     * 初始化
     */
    protected function initialize()
    {
        parent::initialize();
        $this->model = new PermissionGroupModel();
    }

    /**
     * 查看权限组列表
     */
    public function index()
    {
        $filter = $this->request->get('filter', '');
        $filter = (array)json_decode($filter, true);
        $page = $this->request->get('page/d', 1);
        $limit = $this->request->get('limit/d', 10);
        
        // 构建查询条件
        $where = [];
        
        // 按名称筛选
        if (isset($filter['name']) && $filter['name'] !== '') {
            $where[] = ['name', 'like', '%' . $filter['name'] . '%'];
        }
        
        // 按状态筛选
        if (isset($filter['status']) && $filter['status'] !== '') {
            $where[] = ['status', '=', $filter['status']];
        }
        
        // 查询列表
        $list = $this->model->where($where)
            ->page($page, $limit)
            ->order('id', 'desc')
            ->select();
            
        $total = $this->model->where($where)->count();
        
        return $this->success('获取成功', ['list' => $list, 'total' => $total]);
    }

    /**
     * 添加权限组
     */
    public function add()
    {
        $row = $this->request->post('row/a');
        
        // 数据验证
        $validate = Validate::rule([
            'name' => 'require|max:50',
            'description' => 'max:255',
            'status' => 'require|in:0,1',
        ]);
        
        $result = $validate->check($row);
        if (true !== $result) {
            return $this->error($validate->getError());
        }
        
        // 检查权限组名称是否重复
        if ($this->model->where('name', $row['name'])->find()) {
            return $this->error('权限组名称已存在');
        }
        
        // 添加权限组
        $this->model->save($row);
        
        return $this->success('添加成功');
    }

    /**
     * 编辑权限组
     */
    public function edit()
    {
        $row = $this->request->post('row/a');
        $ids = $this->request->param('ids');
        
        if (!$ids) {
            return $this->error('参数错误');
        }
        
        // 数据验证
        $validate = Validate::rule([
            'name' => 'require|max:50',
            'description' => 'max:255',
            'status' => 'require|in:0,1',
        ]);
        
        $result = $validate->check($row);
        if (true !== $result) {
            return $this->error($validate->getError());
        }
        
        // 检查权限组名称是否重复
        $exists = $this->model->where('name', $row['name'])
            ->where('id', '<>', $ids)
            ->find();
        if ($exists) {
            return $this->error('权限组名称已存在');
        }
        
        // 更新权限组
        $this->model->where('id', $ids)->update($row);
        
        return $this->success('编辑成功');
    }

    /**
     * 删除权限组
     */
    public function del()
    {
        $ids = $this->request->param('ids');
        
        if (!$ids) {
            return $this->error('参数错误');
        }
        
        // 检查权限组是否被使用
        $permissionCount = Db::name('yxerp_permission')
            ->where('group_id', 'in', $ids)
            ->count();
            
        if ($permissionCount > 0) {
            return $this->error('该权限组下有权限项，无法删除');
        }
        
        // 删除权限组
        $this->model->where('id', 'in', $ids)->delete();
        
        return $this->success('删除成功');
    }
} 